// define the db model
CultureMuncher_Db = {
	con: new SQLite("cm.sqlite"),
	SelectWorkflow: function(urlparam) {
		return this.con.selectWorkflow({url:urlparam});
	},
	UpdateWorkflow: function(id, workflow) {
		return this.con.updateWorkflow({id:id, workflow:workflow});
	},
	DeleteWorkflow: function(id) {
		return this.con.deleteWorkflow({id:id});
	},
	CreateWorkflow: function(url, workflow, type) {
		var id = this.con.createWorkflow({workflow:workflow});
		this.con.createWorkflowPage({id:id, url:url, type:type});
		return id;
	}
}

// extend the definition
CultureMuncher_Db.con.extend({
	createWorkflow:"INSERT INTO Workflow (Workflow) VALUES ('{workflow}');select last_insert_rowid() AS Id;",
	createWorkflowPage:"INSERT INTO WorkflowPage (WorkflowId, Url, Type) VALUES ({id}, '{url}', {type})",
    selectWorkflow:"SELECT w.*, wp.Type FROM Workflow w, WorkflowPage wp WHERE w.Id = wp.WorkflowId AND Url='{url}'",
    updateWorkflow:"UPDATE Workflow SET Workflow='{workflow}' WHERE Id={id}",
    deleteWorkflow:"DELETE FROM Workflow Where Id={id}"
});

/*
con.extend({
    create:"CREATE TABLE {name} ({columns})",
    insert:"INSERT INTO tb_test (name,age) VALUES('{name}',{age})",
    select:"SELECT * FROM {name}",
    empty:"DELETE FROM tb_test",
    drop:"DROP TABLE IF EXISTS tb_test"
});
con.create({name:'tb_test',columns:"id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, age INTEGER"});
con.insert({name:'Tom',age:29});
con.insert({name:'Bob',age:34});
res = con.select({name:'tb_test'});
for(var i=0;i<res.length;i++){
    alert(res[i].name+":"+res[i].age);
}
con.drop();
 */
